package org.dromara.canteen.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.canteen.domain.vo.CanteenCookingVo;
import org.dromara.canteen.domain.bo.CanteenCookingBo;
import org.dromara.canteen.service.ICanteenCookingService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 烹饪管理
 *
 * @author Gjb
 * @date 2025-07-31
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/canteen/cooking")
public class CanteenCookingController extends BaseController {

    private final ICanteenCookingService canteenCookingService;

    /**
     * 查询烹饪管理列表
     */
    @SaCheckPermission("canteen:cooking:list")
    @GetMapping("/list")
    public TableDataInfo<CanteenCookingVo> list(CanteenCookingBo bo, PageQuery pageQuery) {
        return canteenCookingService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出烹饪管理列表
     */
    @SaCheckPermission("canteen:cooking:export")
    @Log(title = "烹饪管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CanteenCookingBo bo, HttpServletResponse response) {
        List<CanteenCookingVo> list = canteenCookingService.queryList(bo);
        ExcelUtil.exportExcel(list, "烹饪管理", CanteenCookingVo.class, response);
    }

    /**
     * 获取烹饪管理详细信息
     *
     * @param cookingId 主键
     */
    @SaCheckPermission("canteen:cooking:query")
    @GetMapping("/{cookingId}")
    public R<CanteenCookingVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long cookingId) {
        return R.ok(canteenCookingService.queryById(cookingId));
    }

    /**
     * 新增烹饪管理
     */
    @SaCheckPermission("canteen:cooking:add")
    @Log(title = "烹饪管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CanteenCookingBo bo) {
        return toAjax(canteenCookingService.insertByBo(bo));
    }

    /**
     * 修改烹饪管理
     */
    @SaCheckPermission("canteen:cooking:edit")
    @Log(title = "烹饪管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CanteenCookingBo bo) {
        return toAjax(canteenCookingService.updateByBo(bo));
    }

    /**
     * 删除烹饪管理
     *
     * @param cookingIds 主键串
     */
    @SaCheckPermission("canteen:cooking:remove")
    @Log(title = "烹饪管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cookingIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] cookingIds) {
        return toAjax(canteenCookingService.deleteWithValidByIds(List.of(cookingIds), true));
    }
}
